#!/usr/bin/env python
# -*- coding:utf8 -*-
# uncompyle6 version 2.15.1
# Python bytecode 2.7 (62211)
# Decompiled from: Python 2.7.10 (default, Jul  1 2017, 13:36:56) 
# [GCC 4.4.6 20110731 (Red Hat 4.4.6-4)]
# Embedded file name: ./login/bkaccount/views_api.py
# Compiled at: 2017-11-16 15:44:28
"""
Tencent is pleased to support the open source community by making 蓝鲸智云(BlueKing) available.
Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and limitations under the License.
"""
import json
from django.views.decorators.csrf import csrf_exempt
from bkaccount.decorators import login_exempt
from bkaccount.models import BkUser
from bkaccount.validators import validate_password, validate_username, validate_bk_token, is_request_from_esb
from bkaccount.constants import ApiErrorCodeEnum
from bkaccount.utils import gen_success_json, gen_fail_json

@login_exempt
def is_login(request):
    """
    用户登录态验证API
    """
    is_valid, user, msg = validate_bk_token(request.GET)
    if not is_valid:
        return gen_fail_json(ApiErrorCodeEnum.PARAM_NOT_VALID, msg)
    return gen_success_json(u'用户验证成功', {'username': user.username})


@login_exempt
def get_user(request):
    """
    获取用户信息API
    """
    is_valid, user, msg = validate_bk_token(request.GET)
    if not is_valid:
        is_from_esb = is_request_from_esb(request)
        username = request.GET.get('username')
        if not is_from_esb or not username:
            return gen_fail_json(ApiErrorCodeEnum.PARAM_NOT_VALID, msg)
    else:
        username = user.username
    result, data, msg = BkUser.objects.get_user_info(username)
    if not result:
        return gen_fail_json(ApiErrorCodeEnum.USER_NOT_EXISTS2, msg)
    return gen_success_json(u'用户信息获取成功', data)


@login_exempt
def get_all_user(request):
    """
    获取所有用户信息API
    """
    if not is_request_from_esb(request):
        is_valid, user, msg = validate_bk_token(request.GET)
        if not is_valid:
            return gen_fail_json(ApiErrorCodeEnum.PARAM_NOT_VALID, msg)
    role = request.GET.get('role', '')
    data = BkUser.objects.get_all_user(role)
    return gen_success_json(u'用户信息获取成功', data)


@csrf_exempt
@login_exempt
def get_batch_user(request):
    """
    批量获取用户信息API
    参数：username_list:['admin', 'admin1']
    """
    try:
        post_data = json.loads(request.body)
        username_list = post_data.get('username_list')
    except:
        msg = u'请求参数格式错误,必须为json格式'
        return gen_fail_json(ApiErrorCodeEnum.PARAM_NOT_VALID, msg)

    if not is_request_from_esb(request):
        is_valid, user, msg = validate_bk_token(post_data)
        if not is_valid:
            return gen_fail_json(ApiErrorCodeEnum.PARAM_NOT_VALID, msg)
    if not username_list:
        return gen_fail_json(ApiErrorCodeEnum.PARAM_NOT_VALID, u'缺少参数:username_list')
    data = BkUser.objects.get_batch_user_with_dict(username_list)
    return gen_success_json(u'用户信息获取成功', data)


@csrf_exempt
@login_exempt
def reset_password(request):
    """
    重置密码
    """
    try:
        post_data = json.loads(request.body)
    except:
        msg = u'请求参数格式错误,必须为json格式'
        return gen_fail_json(ApiErrorCodeEnum.PARAM_NOT_VALID, msg)

    is_valid, user, msg = validate_bk_token(post_data)
    if not is_valid:
        return gen_fail_json(ApiErrorCodeEnum.PARAM_NOT_VALID, msg)
    username = post_data.get('username')
    if not user.is_superuser and user.username != username:
        msg = u'您的权限不足'
        return gen_fail_json(ApiErrorCodeEnum.PARAM_NOT_VALID, msg)
    new_password = post_data.get('new_password', '').strip()
    is_valid, msg = validate_password(new_password)
    if not is_valid:
        return gen_fail_json(ApiErrorCodeEnum.PARAM_NOT_VALID, msg)
    result, error_code, msg = BkUser.objects.modify_password_by_username(username, new_password)
    if not result:
        return gen_fail_json(error_code, msg)
    return gen_success_json(u'密码重置成功')


@csrf_exempt
@login_exempt
def modify_user_info(request):
    """
    修改用户个人信息
    """
    try:
        post_data = json.loads(request.body)
    except:
        msg = u'请求参数格式错误,必须为json格式'
        return gen_fail_json(ApiErrorCodeEnum.PARAM_NOT_VALID, msg)

    is_valid, user, msg = validate_bk_token(post_data)
    if not is_valid:
        return gen_fail_json(ApiErrorCodeEnum.PARAM_NOT_VALID, msg)
    username = post_data.get('username')
    if not user.is_superuser and user.username != username:
        msg = u'您的权限不足'
        return gen_fail_json(ApiErrorCodeEnum.PARAM_NOT_VALID, msg)
    is_valid, msg = validate_username(username)
    if not is_valid:
        return gen_fail_json(ApiErrorCodeEnum.PARAM_NOT_VALID, msg)
    chname = post_data.get('chname').strip()
    qq = post_data.get('qq').strip()
    phone = post_data.get('phone').strip()
    email = post_data.get('email').strip()
    result, error_code, msg = BkUser.objects.modify_user_info(username, chname, qq, phone, email)
    if not result:
        return gen_fail_json(ApiErrorCodeEnum.PARAM_NOT_VALID, msg)
    return gen_success_json(u'用户信息修改成功')


@csrf_exempt
@login_exempt
def modify_user_role(request):
    """
    修改用户角色
    """
    try:
        post_data = json.loads(request.body)
    except:
        msg = u'请求参数格式错误,必须为json格式'
        return gen_fail_json(ApiErrorCodeEnum.PARAM_NOT_VALID, msg)

    is_valid, user, msg = validate_bk_token(post_data)
    if not is_valid:
        return gen_fail_json(ApiErrorCodeEnum.PARAM_NOT_VALID, msg)
    if not user.is_superuser:
        msg = u'您的权限不足'
        return gen_fail_json(ApiErrorCodeEnum.PARAM_NOT_VALID, msg)
    username = post_data.get('username')
    role = int(post_data.get('role'))
    result, msg = BkUser.objects.modify_user_role(username, role)
    if not result:
        return gen_fail_json(ApiErrorCodeEnum.PARAM_NOT_VALID, msg)
    return gen_success_json(u'用户角色修改成功')
